//---------------------------------------------------------------------------

#ifndef TSimulateH
#define TSimulateH

#include "TTrack.h"
#include "TCut.h"
#include "TCtrRetarder.h"
#include "TArrester.h"
#include "TBooster.h"
#include "TTrackRetarder.h"
#include "TWeather.h"
#include "TSetting.h"
#include "TCommunicate.h"
//#include "TCtrNumber.h"
#include "TSensor.h"
#include "TSpeedTimePosition.h"
#include "TDrawCheckMap.h"
#include "TControl.h"
#include "TArresterMap.h"
#include "TArresterPicture.h"
#include "TStatistics.h"
#include "TStatisticsForm.h"

#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <fstream.h>

//Todo 2 : Check the base resistance on the hump.
//Todo 2 -o Li Wenjian : Repeat code the function of TCtrRetarder::Work for correct the work as the CtrRetarder is down.
//Todo 5 -o Li Wenjian : Use a Pointer to point a point between two Sections.

float NowTime;

//---------------------------------------------------------------------------

class TSimulate
{
private:
	int FormatRight;

	TTracks FTracks;

   TRetarders FRetarders;
   TCtrRetarders FCtrRetarders;
   TArresters FArresters;
   TBoosters FBoosters;

   TControls FControls;
   TCuts FCuts;
   TWeather FWeather;

   TSpeedSensors FSpeedSensors;
   TWeightSensors FWeightSensors;
   TCircuits FCircuits;

   TSetting FSetting;

   TCommunicate Comm;
//   TCtrNumbers CtrNumbers;
   TVTS FVTS;




	bool Init;
	TMeasurementRate MeasurementRate;
	eMeasurement Measurement;

	TDateTime StartTime;
	TDateTime CurrentTime;

	float SimTime;
	float EndTime;

   void __fastcall Receive(void);
   void __fastcall SetMessage(unsigned short CutNumber);
	void __fastcall Step(const unsigned short CutNumber);
	void __fastcall SetCutSpeed(const unsigned short Index, const float Speed);
	void __fastcall SetFrontCutSpeed(const int Index, const float Speed);
   void __fastcall SetCutPreSpeed(const unsigned short Index, const float Speed);
   void __fastcall AddCutTime(const unsigned short Index, const float Time);

   TSpeedTimePosition __fastcall GetSpeedTimePosition(const unsigned short Index);

   float __fastcall GetRetarderWork(const unsigned short Index);
   float __fastcall GetCutWeight(const unsigned short Index);
	float __fastcall GetCutWork(const unsigned short Index);
	float __fastcall GetCutWork(const int Index, const float CutForce);

	float __fastcall CutWeight(const unsigned short Index);
	float __fastcall CutWork(const unsigned short Index);
	float __fastcall GetCutForce(const unsigned short Index);
	bool __fastcall IsDisengage(const int Index, const float CutForce);
	bool __fastcall GetDisengage(const int Index);
	float __fastcall CutCurveForce(const unsigned short Index);
	float __fastcall CutSwitchForce(const unsigned short Index);
	float __fastcall CutGradeForce(const unsigned short Index);
	float __fastcall CutGradeForce(const unsigned short CutI, const unsigned short WagonI);

   void __fastcall MoveCut(const unsigned short Index);
   bool __fastcall IsMeet(const unsigned short Index);
   void __fastcall WaitTo(TDateTime WaitToTime);
   void __fastcall ShowCutsMessage(const unsigned short CutNumber);
   bool __fastcall CutMessageViewInitialize(void);
   void __fastcall ShowCtrState(void);
   void __fastcall ShowCtrState(const unsigned short CtrRetarderNumber);
   bool __fastcall ShowCtrStateInitialize(void);
   void __fastcall UpdateCtrState(const unsigned short Cut);

   void __fastcall SetCheckImage(TImage * CheckMap);
   void __fastcall SetCheckMapXDPI(unsigned short value);
   void __fastcall AddVTS(const unsigned short CutID);

public:
   TTrackRetarders TrackRetarders;

//	TSwitchStat SwitchStat;
	TStatistics *Statistics;

   __property TTracks Tracks = { read=FTracks, write=FTracks };

   __property TRetarders Retarders = { read=FRetarders, write=FRetarders };
   __property TCtrRetarders CtrRetarders = { read=FCtrRetarders, write=FCtrRetarders };
   __property TArresters Arresters = { read=FArresters, write=FArresters };
   __property TBoosters Boosters = { read=FBoosters, write=FBoosters };

   __property TControls Controls = { read=FControls, write=FControls };

   __property TWeightSensors WeightSensors = { read=FWeightSensors, write=FWeightSensors };
   __property TSpeedSensors SpeedSensors = { read=FSpeedSensors, write=FSpeedSensors };
   __property TCircuits Circuits = { read=FCircuits, write=FCircuits };

   __property TSetting Setting = { read=FSetting, write=FSetting };

   __property TWeather Weather = { read=FWeather, write=FWeather };

   __property TCuts Cuts = { read=FCuts, write=FCuts };

   __property TVTS VTS = { read=FVTS };

   __property unsigned short CheckMapXDPI = { write=SetCheckMapXDPI };

   unsigned short DrawTrack;
	TImage *ShowImage;

	TCanvas *FormCanvas;
   TDrawCheckMap DrawCheckMap;
	TImage *ShowArresterMap;
//	TArresterMap ArresterMap;
	TArresterPicture ArresterPicture;
	int BitmapWidth, BitmapHeight;
	int Top, Left;
	bool Stop, Pause;

   TListBox *InputListBox, *OutputListBox;
   TListView * CutMessageListView;
   TListView * CtrStateListView;

	__fastcall TSimulate();
	bool __fastcall CheckDataMistake(AnsiString &CheckMessage);
   bool __fastcall Initialize(AnsiString & InitMessage);
	void __fastcall SetCtrState(unsigned short CtrNumber);
	void __fastcall SetParameter(void);
   void __fastcall Run(void);
   void __fastcall CheckRun(void);
   void __fastcall ConnectRun(void);
	void __fastcall RunCheck(eMoveModel MoveModel);
	void __fastcall RunSim(void);

	bool __fastcall SetCtrState(unsigned short i, bool State);
	bool __fastcall GetStatResult(void);

   void __fastcall AddCtrRetarder(const AnsiString Model,
                                  const float Begin, const float End,
                                  const float CriticalSpeed, const float CriticalSpeedWindage,
                                  const unsigned short Quantity, const  unsigned  short Number,
                                  const unsigned short MinTrack, const unsigned short MaxTrack);

	void __fastcall DrawWholeCheckMap(TImage *Image);  //Todo 2 : Draw which chech map;
	void __fastcall ExportWholeCheckMap(const AnsiString FileName);
	void __fastcall DrawWholeCheckMap(Graphics::TCanvas *Canvas, const unsigned short Height, const unsigned short Width);
	void __fastcall DrawInitCheckAnimal(void);
	void __fastcall DrawInitCheckMap(TCanvas *Canvas, const unsigned short TrackNumber);
	void __fastcall DrawInitArresterMap(void);
   void __fastcall DrawCheckTimeSpeedPosition(const unsigned short CutIndex);

   void __fastcall DrawArresterInitMap(void);
   void __fastcall DrawArresterCut(const unsigned short Cut);
	void __fastcall DrawArresterMap(const unsigned short TrackNumber);
	void __fastcall DrawWholeArresterPicture(void);

	void __fastcall ShowCheckMap(TImage *ShowImage);
	bool __fastcall ImportFromFor(const AnsiString FileName);
	bool __fastcall ImportFromDat(const AnsiString FileName, AnsiString &Message);
  	bool __fastcall ReadLineFromForFile(ifstream *ForFile, AnsiString &Line);
};

#endif
